package com.vgmobile.cucrever2;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.special.ResideMenu.ResideMenu;
import com.special.ResideMenu.ResideMenuItem;
import com.vgmobile.cucrever2.account.AccountFragment;
import com.vgmobile.cucrever2.global.BaseActivity;
import com.vgmobile.cucrever2.home.HomeFragment;
import com.vgmobile.cucrever2.mapstore.MapStoreActivity;
import com.vgmobile.cucrever2.order.OrderFragment;
import com.vgmobile.cucrever2.search.SearchActivity;

public class ResideMenuActivity extends BaseActivity implements OnClickListener {

	private ResideMenu resideMenu;
	private ResideMenuActivity activity;
	private ResideMenuItem itemMenuHome;
	private LinearLayout btnHome, btnStore, btnOrder, btnUser;
	private FrameLayout homeLayout, orderLayout, accountLayout;
	private TextView txtTitle, txtSearch;
	private String titleOld;
	private Button btnSlideLeft, btnBack, btnCartRight;
	private Fragment currentFragment, homeFragment, orderFragment, accountFragment;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.reside_menu_actitvity);
		activity = this;

		setupImageLoad();
		setUpMenu();
		
		homeLayout = (FrameLayout) findViewById(R.id.reside_home_fragment);
		orderLayout = (FrameLayout) findViewById(R.id.reside_order_fragment);
		accountLayout = (FrameLayout) findViewById(R.id.reside_account_fragment);
		resideMenu.addIgnoredView(homeLayout);
		resideMenu.addIgnoredView(orderLayout);
		resideMenu.addIgnoredView(accountLayout);
		

		btnSlideLeft = (Button) findViewById(R.id.menu_bar_btnleft);
		btnBack = (Button) findViewById(R.id.menu_bar_btnback);
		btnCartRight = (Button) findViewById(R.id.menu_bar_btnright);
		txtTitle = (TextView) findViewById(R.id.menu_bar_text);
		txtSearch = (TextView) findViewById(R.id.menu_bar_search);
		
		btnHome = (LinearLayout) findViewById(R.id.reside_barbottom_btn_home);
		btnStore = (LinearLayout) findViewById(R.id.reside_barbottom_btn_store);
		btnOrder = (LinearLayout) findViewById(R.id.reside_barbottom_btn_order);
		btnUser = (LinearLayout) findViewById(R.id.reside_barbottom_btn_user);

		btnHome.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
//				if(currentFragment != homeFragment){
//					setSelectTab(0);
//				}

				setSelectTab(0);
			}
		});
		btnStore.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(activity, MapStoreActivity.class);
				startActivity(intent);
			    overridePendingTransition(R.anim.activity_fade_in, R.anim.activity_fade_out);
			}
		});
		btnOrder.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(currentFragment != orderFragment){
					setSelectTab(2);
				}
			}
		});
		btnUser.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				
			}
		});

		setSelectTab(0);
	}

	public void setSelectTab(int index){
		switch (index) {
		case 0:
			if(homeFragment == null){
				homeFragment = new HomeFragment();
				updateFragment(homeFragment, R.id.reside_home_fragment);
			}
	    	currentFragment = homeFragment;
	    	
	    	if(!((HomeFragment)homeFragment).isRunning()){
	    		if(btnSlideLeft.getVisibility() == View.VISIBLE){
		    		setTitleMenuHome(titleOld);
	    		}else{
		    		setTitleMenuHome(null);
	    			getSupportFragmentManager().popBackStack(TAG_FRAGMENT_HOME, FragmentManager.POP_BACK_STACK_INCLUSIVE);
	    		}
	    		
	    	}else{
		    	setTitleMenuHome(null);
	    	}
	    	
			btnHome.setSelected(true);
			btnStore.setSelected(false);
			btnOrder.setSelected(false);
			btnUser.setSelected(false);
			
			homeLayout.setVisibility(View.VISIBLE);			
			orderLayout.setVisibility(View.GONE);			
			accountLayout.setVisibility(View.GONE);
			break;
		case 2:
			if(orderFragment == null){
				orderFragment = new OrderFragment();
				updateFragment(orderFragment, R.id.reside_order_fragment);
			}
	    	currentFragment = orderFragment;
	    	setTitleMenuOrder("Đơn hàng");
			btnHome.setSelected(false);
			btnStore.setSelected(false);
			btnOrder.setSelected(true);
			btnUser.setSelected(false);
			
			homeLayout.setVisibility(View.GONE);			
			orderLayout.setVisibility(View.VISIBLE);			
			accountLayout.setVisibility(View.GONE);
			break;
		case 3:
			if(accountFragment == null){
				accountFragment = new AccountFragment();
				updateFragment(accountFragment, R.id.reside_account_fragment);
			}
	    	currentFragment = accountFragment;
	    	setTitleMenuHome(null);
			btnHome.setSelected(false);
			btnStore.setSelected(false);
			btnOrder.setSelected(false);
			btnUser.setSelected(true);
			
			homeLayout.setVisibility(View.GONE);			
			orderLayout.setVisibility(View.GONE);			
			accountLayout.setVisibility(View.VISIBLE);
			break;
		}
	}
	
	public void setTitleMenuHome(String title){
		if(title == null){
			btnBack.setVisibility(View.GONE);
			btnSlideLeft.setVisibility(View.VISIBLE);
			txtTitle.setVisibility(View.GONE);
			txtSearch.setVisibility(View.VISIBLE);
		}else{
			titleOld = title;
			btnBack.setVisibility(View.VISIBLE);
			btnSlideLeft.setVisibility(View.GONE);
			txtTitle.setVisibility(View.VISIBLE);
			txtSearch.setVisibility(View.GONE);
			txtTitle.setText(title);
		}
	}
	
	public void setTitleMenuOrder(String title){
		btnBack.setVisibility(View.GONE);
		btnSlideLeft.setVisibility(View.VISIBLE);
		txtTitle.setVisibility(View.VISIBLE);
		txtSearch.setVisibility(View.GONE);
		txtTitle.setText(title);
	}
	
	private void setUpMenu() {
		resideMenu = new ResideMenu(activity);
		resideMenu.setBackground(R.drawable.background);
		resideMenu.attachToActivity(activity);
		resideMenu.setMenuListener(menuListener);

		itemMenuHome = new ResideMenuItem(activity, R.drawable.null_stubload, "Trang chủ");
		itemMenuHome.setOnClickListener(this);
		resideMenu.addMenuItem(itemMenuHome, ResideMenu.DIRECTION_LEFT);

		resideMenu.setDirectionDisable(ResideMenu.DIRECTION_RIGHT);

		findViewById(R.id.menu_bar_btnleft).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						resideMenu.openMenu(ResideMenu.DIRECTION_LEFT);
					}
				});
	}
	
	public void openSearch(View view){
		Intent intent = new Intent(this, SearchActivity.class);
		startActivity(intent);
	    overridePendingTransition(R.anim.activity_fade_in, R.anim.activity_fade_out);
	}
	
	@Override
	public void onClick(View view) {
//		if(view.equals(itemTrangChu)){
//			if(!currentFragment.equals(homeFragment)){
//				homeFragment = new HomeFragment(this);
//				updateFragment(homeFragment);
//			}
//		}else if(view.equals(itemGioHang)){
//			if(!currentFragment.equals(cartFragment)){
//				cartFragment = new CartFragment();
//				updateFragment(cartFragment);
//			}
//		}
		resideMenu.closeMenu();
	}
	
	@Override
	public void onBackPressed() {
		if(resideMenu.isOpened()){
			resideMenu.closeMenu();
		}else{
			if(currentFragment == orderFragment){
				finish();
			}else{
				super.onBackPressed();
			}
		}
	}

	private ResideMenu.OnMenuListener menuListener = new ResideMenu.OnMenuListener() {
		@Override
		public void openMenu() {

		}

		@Override
		public void closeMenu() {

		}
	};
	
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return resideMenu.onInterceptTouchEvent(ev) || super.dispatchTouchEvent(ev);
    }
	
    private void updateFragment(Fragment fragment, int index){
    	FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
        .replace(index, fragment, String.valueOf(index))
        .commit();
    }
	
    public ResideMenu getResideMenu(){
        return resideMenu;
    }
	
	public void nextFragment(Fragment fragment, String tag){
		FragmentTransaction transaction = getSupportFragmentManager().beginTransaction()
					.setCustomAnimations(R.anim.activity_fade_in, R.anim.activity_fade_out, R.anim.activity_back_fade_in, R.anim.activity_back_fade_out);
    	transaction.replace(R.id.reside_home_fragment, fragment, String.valueOf(R.id.reside_home_fragment));
    	transaction.addToBackStack(tag);
    	transaction.commit();
	}
	
}
